package com.mz.mattress.client.api.biz;

import cn.hutool.json.JSONUtil;
import com.mz.mattress.client.api.resp.DeviceStatusResp;
import com.mz.mattress.client.service.impl.MattressMsgServiceImpl;
import com.mz.mattress.client.util.SseEmitterUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;

@Slf4j
@RestController
@RequestMapping("/sse")
public class SseController {

    /**
     * 用于创建连接
     */
    @GetMapping("/connect/{userId}")
    public SseEmitter connect(@PathVariable Long userId) {
        return SseEmitterUtil.connect(userId);
    }

    /**
     * 关闭连接
     */
    @GetMapping("/close/{userId}")
    public void close(@PathVariable("userId") Long userId) {
        SseEmitterUtil.removeUser(userId);
    }

    @GetMapping("/send/{memberId}/{val}")
    public void send(@PathVariable Long memberId, @PathVariable Integer val) {

        DeviceStatusResp resp = new DeviceStatusResp();
        resp.setId(3);
        resp.setDeviceCode("67738bd8353aa6e11d5c96a3");
        resp.setSignal(2);
        resp.setOnline(1);
        resp.setWorkMode(3);
        resp.setWorkStatus(1);
        resp.setTemper(33);
        resp.setTemperCur(24);
        resp.setStrength(8);
        resp.setVolume(9);

        resp.setRate(111);
        resp.setOxygen(55);
        resp.setCycle(99);
        resp.setTired(28);
        resp.setResults(4);
        resp.setTemperCur(29);
        resp.setInspectStatus(val);
        resp.setEndTime("2025-03-14 17:55:20");

//        Goods goods = new Goods();
//        goods.setTitle("ddd");
//        goods.setCreateTime(DateUtil.getNowDateTime());
//        SseEmitterUtil.sendMessage(memberId, JSONUtil.toJsonStr(resp));

        MattressMsgServiceImpl.MAP_TIMES.put(1, resp);

    }

}
